package com.cocos.game;
import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.cocos.lib.CocosHelper;
import com.cocos.lib.JsbBridgeWrapper;

import java.util.UUID;

public class DeviceUtils {
    public static String getDeviceId() {
        JsbBridgeWrapper jbw = JsbBridgeWrapper.getInstance();
        Context context = CocosHelper.getActivity().getApplicationContext();

        final SharedPreferences prefs = context.getSharedPreferences("device_id.xml", 0);
         String id = prefs.getString("device_id", null );

        if (id == null) {
            final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            if (TextUtils.isEmpty(androidId) || TextUtils.equals("9774d56d682e549c", androidId)) {
                id = UUID.randomUUID().toString();
            } else {
                id = new UUID(androidId.hashCode(), ((long) androidId.hashCode() << 32)).toString();
            }
            id = id.replace("-", "");
            // Write the value out to the prefs file
            prefs.edit().putString("device_id", id).apply();
        }
        return id;
    }
}
